package com.dingwei.superiordiary.adapter.easyViewHolder;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bobomee.android.mentions.edit.util.FormatRangeManager;
import com.bobomee.android.mentions.text.MentionTextView;
import com.bumptech.glide.Glide;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.FindDetailsPicAdapter;
import com.dingwei.superiordiary.app.MyApplication;
import com.dingwei.superiordiary.bean.LookUserCenterBean;
import com.dingwei.superiordiary.parser.Parser;
import com.dingwei.superiordiary.ui.activity.FindDetailsActivity;
import com.dingwei.superiordiary.ui.activity.PhotoDetailsActivity;
import com.dingwei.superiordiary.utils.SampleListener;
import com.dingwei.superiordiary.weight.SampleCoverVideo1;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.weight.MyGridView;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.lihao.baselibrary.base.BaseActivity.context;

/**
 * Created time 2018/1/30.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class UserCenterListAdapter extends RecyclerArrayAdapter<LookUserCenterBean.DataBean.ListBean> {

    FormatRangeManager mRangeManager;

    public interface xxInterFace {
        void showRightDialog(int position, View view);

        void showShareDialog(int position, View view);

        void likes(int position);

        void forward(int position);

    }

    private xxInterFace xxInterfaceListener;

    public void xxInterfaceListener(xxInterFace listener) {
        xxInterfaceListener = listener;
    }

    private Parser mTagParser = new Parser();

    public UserCenterListAdapter(Context context) {
        super(context);
        mRangeManager = new FormatRangeManager();
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new FindListHolder(parent);
    }


    class FindListHolder extends BaseViewHolder<LookUserCenterBean.DataBean.ListBean> {
        GSYVideoOptionBuilder gsyVideoOptionBuilder;
        @Bind(R.id.img_head)
        ImageView imgHead;
        @Bind(R.id.tv_nick_name)
        TextView tvNickName;
        @Bind(R.id.tv_level)
        TextView tvLevel;
        @Bind(R.id.tv_add_time)
        TextView tvAddTime;
        @Bind(R.id.tv_ding)
        TextView tvDing;
        @Bind(R.id.img_more)
        ImageView imgMore;
        @Bind(R.id.tv_content)
        MentionTextView tvContent;
        @Bind(R.id.tv_forward_content)
        MentionTextView tvForwardContent;
        @Bind(R.id.video_item_player)
        SampleCoverVideo1 videoItemPlayer;
        @Bind(R.id.gridview)
        MyGridView gridview;
        @Bind(R.id.img_size)
        TextView imgSize;
        @Bind(R.id.img_layout)
        LinearLayout imgLayout;
        @Bind(R.id.more_img_layout)
        RelativeLayout moreImgLayout;
        @Bind(R.id.img_content)
        ImageView imgContent;
        @Bind(R.id.tv_posts_type)
        TextView tvPostsType;
        @Bind(R.id.forward_layout)
        LinearLayout forwardLayout;
        @Bind(R.id.img_likes)
        ImageView imgLikes;
        @Bind(R.id.tv_zan)
        TextView tvZan;
        @Bind(R.id.zan_layout)
        LinearLayout zanLayout;
        @Bind(R.id.tv_xiaoxi)
        TextView tvXiaoxi;
        @Bind(R.id._xiaoxi_layout)
        LinearLayout XiaoxiLayout;
        @Bind(R.id.tv_zhaunfa)
        TextView tvZhaunfa;
        @Bind(R.id.zhuanfa_layout)
        LinearLayout zhuanfaLayout;
        @Bind(R.id.share)
        TextView share;
        @Bind(R.id.share_layout)
        LinearLayout shareLayout;
        @Bind(R.id.tv_forward_time)
        TextView tvForwardTime;
        @Bind(R.id.tv_forward_source)
        TextView tvForwardSource;


        ImageView imageView;
        @Bind(R.id.forward_layout_bottom)
        LinearLayout forwardLayoutBottom;
        @Bind(R.id.tv_posts_type1)
        TextView tvPostsType1;
        @Bind(R.id.head_layout)
        LinearLayout headLayout;

        public FindListHolder(ViewGroup parent) {
            super(parent, R.layout.item_user_center);
            ButterKnife.bind(this, itemView);
            imageView = new ImageView(context);
            gsyVideoOptionBuilder = new GSYVideoOptionBuilder();
        }

        @Override
        public void setData(final LookUserCenterBean.DataBean.ListBean data) {
            super.setData(data);

            tvContent.setMovementMethod(new LinkMovementMethod());
            tvContent.setParserConverter(mTagParser);
            tvForwardContent.setMovementMethod(LinkMovementMethod.getInstance());
            tvForwardContent.setParserConverter(mTagParser);

            if (data.top.equals("1")) {
                tvDing.setVisibility(View.VISIBLE);
            } else {
                tvDing.setVisibility(View.GONE);
            }

            tvZan.setText(data.likes);
            tvXiaoxi.setText(data.comments);
            tvZhaunfa.setText(data.transpond);
            share.setText(data.share);

            if (data.top.equals("0")) {
                tvDing.setVisibility(View.GONE);
            } else {
                tvDing.setVisibility(View.VISIBLE);
            }


            if (data.source == 0) {
                tvForwardSource.setText("");
                tvPostsType.setVisibility(View.VISIBLE);
                shareLayout.setVisibility(View.VISIBLE);
                share.setText(data.share);

                Glide.with(BaseActivity.context)
                        .load(data.portrait)
                        .apply(MyApplication.headOptions)
                        .into(imgHead);
                tvNickName.setText(data.nickname);
                tvAddTime.setText(data.addtime);
                tvAddTime.setVisibility(View.VISIBLE);
                tvLevel.setText("LV." + data.level);
                tvLevel.setVisibility(View.VISIBLE);
                String s = data.content.replaceAll("'>", "'>@");
                tvContent.setText(mRangeManager.getFormatCharSequence(s));

                if (data.istranspond.equals("0")) {
                    tvPostsType1.setVisibility(View.VISIBLE);
                    if (data.type == 0) {
                        tvPostsType1.setText(" 综合 ");
                    } else {
                        tvPostsType1.setText("个人说");
                    }
                } else {
                    tvPostsType1.setVisibility(View.GONE);
                }


            } else {
                shareLayout.setVisibility(View.GONE);
                tvPostsType.setVisibility(View.VISIBLE);
                if (data.source == 1) {
                    tvForwardSource.setText("微博");
                } else {
                    tvForwardSource.setText("哔哩哔哩");
                }

                Glide.with(BaseActivity.context)
                        .load(data.portrait)
                        .apply(MyApplication.headOptions)
                        .into(imgHead);
                tvNickName.setText(data.nickname);
                tvAddTime.setVisibility(View.VISIBLE);
                tvLevel.setVisibility(View.VISIBLE);
                String s = data.content.replaceAll("'>", "'>@");
                tvContent.setText(mRangeManager.getFormatCharSequence(s));

                tvPostsType.setVisibility(View.GONE);
                imgMore.setVisibility(View.VISIBLE);

            }

            if (data.istranspond.equals("0")) {
                forwardLayout.setBackgroundColor(ContextCompat.getColor(BaseActivity.context, R.color.white));
                tvForwardContent.setVisibility(View.GONE);
                tvForwardTime.setVisibility(View.GONE);
                forwardLayoutBottom.setVisibility(View.GONE);
                if (TextUtils.isEmpty(data.video)) {
                    videoItemPlayer.setVisibility(View.GONE);
                    if (data.image != null && data.image.size() > 0) {
                        if (data.image.size() == 1) {
                            imgContent.setVisibility(View.VISIBLE);
                            moreImgLayout.setVisibility(View.GONE);
                            Glide.with(BaseActivity.context)
                                    .load(data.image.get(0))
                                    .apply(MyApplication.options)
                                    .into(imgContent);
                        } else if (data.image.size() < 3) {
                            moreImgLayout.setVisibility(View.VISIBLE);
                            imgLayout.setVisibility(View.GONE);
                            imgContent.setVisibility(View.GONE);
                            FindDetailsPicAdapter adapter = new FindDetailsPicAdapter(BaseActivity.context, data.image);
                            gridview.setAdapter(adapter);
                        } else {
                            moreImgLayout.setVisibility(View.VISIBLE);
                            imgLayout.setVisibility(View.VISIBLE);
                            imgContent.setVisibility(View.GONE);
                            imgSize.setText(data.image.size() + "");
                            List<String> list = new ArrayList<>();
                            list.add(data.image.get(0));
                            list.add(data.image.get(1));
                            list.add(data.image.get(2));
                            FindDetailsPicAdapter adapter = new FindDetailsPicAdapter(BaseActivity.context, list);
                            gridview.setAdapter(adapter);
                        }
                    } else {
                        imgContent.setVisibility(View.GONE);
                        moreImgLayout.setVisibility(View.GONE);
                    }

                } else {
                    moreImgLayout.setVisibility(View.GONE);
                    imgContent.setVisibility(View.GONE);
                    videoItemPlayer.setVisibility(View.VISIBLE);
                    imgContent.setVisibility(View.GONE);
                    //增加封面
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    Glide.with(BaseActivity.context)
                            .load(data.video)

                            .into(imageView);
                    if (imageView.getParent() != null) {
                        ViewGroup viewGroup = (ViewGroup) imageView.getParent();
                        viewGroup.removeView(imageView);
                    }
                    videoItemPlayer.setEnlargeImageRes(R.drawable.jz_enlarge);
                    videoItemPlayer.setShrinkImageRes(R.drawable.jz_shrink);
                    gsyVideoOptionBuilder
                            .setIsTouchWiget(false)
                            .setThumbImageView(imageView)
                            .setUrl(data.video)
                            .setSetUpLazy(true)//lazy可以防止滑动卡顿
                            .setVideoTitle("")
                            .setCacheWithPlay(true)
                            .setRotateViewAuto(false)
                            .setRotateWithSystem(true)
                            .setLooping(true)
                            .setLockLand(false)
                            .setBottomProgressBarDrawable(BaseActivity.context.getResources().getDrawable(R.drawable.video_new_progress))
                            .setBottomShowProgressBarDrawable(BaseActivity.context.getResources().getDrawable(R.drawable.video_new_seekbar_progress), BaseActivity.context.getResources().getDrawable(R.drawable.video_new_seekbar_thumb))
                            .setPlayTag("yy" + getDataPosition())
                            .setShowFullAnimation(false)
                            .setNeedLockFull(false)
                            .setPlayPosition(getDataPosition())
                            .setStandardVideoAllCallBack(new SampleListener() {
                                @Override
                                public void onPrepared(String url, Object... objects) {
                                    super.onPrepared(url, objects);
                                    if (!videoItemPlayer.isIfCurrentIsFullscreen()) {
                                        //静音
                                        GSYVideoManager.instance().setNeedMute(true);
                                    }

                                }

                                @Override
                                public void onQuitFullscreen(String url, Object... objects) {
                                    super.onQuitFullscreen(url, objects);
                                    //全屏不静音
                                    GSYVideoManager.instance().setNeedMute(true);
                                }

                                @Override
                                public void onEnterFullscreen(String url, Object... objects) {
                                    super.onEnterFullscreen(url, objects);
                                    GSYVideoManager.instance().setNeedMute(false);
                                    videoItemPlayer.getCurrentPlayer().getTitleTextView().setText((String) objects[0]);
                                }
                            }).build(videoItemPlayer);

                    //增加title
                    videoItemPlayer.getTitleTextView().setVisibility(View.GONE);

                    //设置返回键
                    videoItemPlayer.getBackButton().setVisibility(View.GONE);

                    //设置全屏按键功能
                    videoItemPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            resolveFullBtn(videoItemPlayer);
                        }
                    });
                }


            } else {
                forwardLayout.setBackgroundColor(ContextCompat.getColor(BaseActivity.context, R.color.forward_bg));
                tvForwardTime.setText(data.transpond_content.addtime);
                tvForwardTime.setVisibility(View.VISIBLE);
                forwardLayoutBottom.setVisibility(View.VISIBLE);
                if (data.transpond_content.type == 0) {
                    tvPostsType.setText(" 综合 ");
                } else {
                    tvPostsType.setText("个人说");
                }

                String s = data.transpond_content.content.replaceAll("'>", "'>@");
                s = " <user id='" + data.transpond_content.consumer_id + "' name='" + data.transpond_content.nickname + "'>@" + data.transpond_content.nickname + "</user> " + s;
                tvForwardContent.setText(mRangeManager.getFormatCharSequence(s));
                if (TextUtils.isEmpty(data.transpond_content.video)) {
                    videoItemPlayer.setVisibility(View.GONE);
                    if (data.transpond_content.image != null && data.transpond_content.image.size() > 0) {
                        if (data.transpond_content.image.size() == 1) {
                            imgContent.setVisibility(View.VISIBLE);
                            moreImgLayout.setVisibility(View.GONE);
                            Glide.with(BaseActivity.context)
                                    .load(data.transpond_content.image.get(0))
                                    .apply(MyApplication.options)
                                    .into(imgContent);
                        } else if (data.transpond_content.image.size() < 3) {
                            moreImgLayout.setVisibility(View.VISIBLE);
                            imgLayout.setVisibility(View.GONE);
                            imgContent.setVisibility(View.GONE);
                            FindDetailsPicAdapter adapter = new FindDetailsPicAdapter(BaseActivity.context, data.transpond_content.image);
                            gridview.setAdapter(adapter);
                        } else {
                            moreImgLayout.setVisibility(View.VISIBLE);
                            imgLayout.setVisibility(View.VISIBLE);
                            imgContent.setVisibility(View.GONE);
                            imgSize.setText(data.transpond_content.image.size() + "");
                            List<String> list = new ArrayList<>();
                            list.add(data.transpond_content.image.get(0));
                            list.add(data.transpond_content.image.get(1));
                            list.add(data.transpond_content.image.get(2));
                            FindDetailsPicAdapter adapter = new FindDetailsPicAdapter(BaseActivity.context, list);
                            gridview.setAdapter(adapter);
                        }
                    } else {
                        imgContent.setVisibility(View.GONE);
                        moreImgLayout.setVisibility(View.GONE);
                    }
                } else {
                    moreImgLayout.setVisibility(View.GONE);
                    imgContent.setVisibility(View.GONE);
                    videoItemPlayer.setVisibility(View.VISIBLE);
                    imgContent.setVisibility(View.GONE);
                    //增加封面
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    Glide.with(BaseActivity.context)
                            .load(data.transpond_content.video)
                            .into(imageView);
                    if (imageView.getParent() != null) {
                        ViewGroup viewGroup = (ViewGroup) imageView.getParent();
                        viewGroup.removeView(imageView);
                    }
                    videoItemPlayer.setEnlargeImageRes(R.drawable.jz_enlarge);
                    videoItemPlayer.setShrinkImageRes(R.drawable.jz_shrink);
                    gsyVideoOptionBuilder
                            .setIsTouchWiget(false)
                            .setThumbImageView(imageView)
                            .setUrl(data.video)
                            .setSetUpLazy(true)//lazy可以防止滑动卡顿
                            .setVideoTitle("")
                            .setCacheWithPlay(true)
                            .setRotateViewAuto(false)
                            .setRotateWithSystem(true)
                            .setLooping(true)
                            .setLockLand(false)
                            .setBottomProgressBarDrawable(BaseActivity.context.getResources().getDrawable(R.drawable.video_new_progress))
                            .setBottomShowProgressBarDrawable(BaseActivity.context.getResources().getDrawable(R.drawable.video_new_seekbar_progress), BaseActivity.context.getResources().getDrawable(R.drawable.video_new_seekbar_thumb))
                            .setPlayTag("yy" + getDataPosition())
                            .setShowFullAnimation(false)
                            .setNeedLockFull(false)
                            .setPlayPosition(getDataPosition())
                            .setStandardVideoAllCallBack(new SampleListener() {
                                @Override
                                public void onPrepared(String url, Object... objects) {
                                    super.onPrepared(url, objects);
                                    if (!videoItemPlayer.isIfCurrentIsFullscreen()) {
                                        //静音
                                        GSYVideoManager.instance().setNeedMute(true);
                                    }

                                }

                                @Override
                                public void onQuitFullscreen(String url, Object... objects) {
                                    super.onQuitFullscreen(url, objects);
                                    //全屏不静音
                                    GSYVideoManager.instance().setNeedMute(true);
                                }

                                @Override
                                public void onEnterFullscreen(String url, Object... objects) {
                                    super.onEnterFullscreen(url, objects);
                                    GSYVideoManager.instance().setNeedMute(false);
                                    videoItemPlayer.getCurrentPlayer().getTitleTextView().setText((String) objects[0]);
                                }
                            }).build(videoItemPlayer);

                    //增加title
                    videoItemPlayer.getTitleTextView().setVisibility(View.GONE);

                    //设置返回键
                    videoItemPlayer.getBackButton().setVisibility(View.GONE);

                    //设置全屏按键功能
                    videoItemPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            resolveFullBtn(videoItemPlayer);
                        }
                    });
                }
            }
            gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Intent intent = new Intent();
                    intent.setClass(context, PhotoDetailsActivity.class);
                    intent.putExtra("position", i);
                    intent.putExtra("img", (Serializable) data.image);
                    BaseActivity.context.startActivity(intent);
                }
            });

            if (data.is_likes == 0) {
                imgLikes.setImageResource(R.mipmap.ic_index_zan);
            } else {
                imgLikes.setImageResource(R.mipmap.ic_liked);
            }

            imgMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (xxInterfaceListener != null) {
                        xxInterfaceListener.showRightDialog(getDataPosition(), imgMore);
                    }
                }
            });
            shareLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (xxInterfaceListener != null) {
                        xxInterfaceListener.showShareDialog(getDataPosition(), shareLayout);
                    }
                }
            });
            zanLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (xxInterfaceListener != null) {
                        xxInterfaceListener.likes(getDataPosition());
                    }
                }
            });
            zhuanfaLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (xxInterfaceListener != null) {
                        xxInterfaceListener.forward(getDataPosition());
                    }
                }
            });


            tvForwardContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (data.istranspond.equals("1")) {
                        Intent intent = new Intent(BaseActivity.context, FindDetailsActivity.class);
                        intent.putExtra("id", data.transpond_content.id);
                        BaseActivity.context.startActivity(intent);
                    } else {
                        Intent intent = new Intent(BaseActivity.context, FindDetailsActivity.class);
                        intent.putExtra("id", data.id);
                        BaseActivity.context.startActivity(intent);
                    }
                }
            });
            tvContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(BaseActivity.context, FindDetailsActivity.class);
                    intent.putExtra("id", data.id);
                    BaseActivity.context.startActivity(intent);
                }
            });
            headLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(BaseActivity.context, FindDetailsActivity.class);
                    intent.putExtra("id", data.id);
                    BaseActivity.context.startActivity(intent);
                }
            });
            forwardLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (data.istranspond.equals("1")) {
                        Intent intent = new Intent(BaseActivity.context, FindDetailsActivity.class);
                        intent.putExtra("id", data.transpond_content.id);
                        BaseActivity.context.startActivity(intent);
                    } else {
                        Intent intent = new Intent(BaseActivity.context, FindDetailsActivity.class);
                        intent.putExtra("id", data.id);
                        BaseActivity.context.startActivity(intent);
                    }
                }
            });
            gridview.setOnTouchBlankPositionListener(new MyGridView.OnTouchBlankPositionListener() {
                @Override
                public void onTouchBlank(MotionEvent event) {
                    if (data.istranspond.equals("1")) {
                        Intent intent = new Intent(BaseActivity.context, FindDetailsActivity.class);
                        intent.putExtra("id", data.transpond_content.id);
                        BaseActivity.context.startActivity(intent);
                    } else {
                        Intent intent = new Intent(BaseActivity.context, FindDetailsActivity.class);
                        intent.putExtra("id", data.id);
                        BaseActivity.context.startActivity(intent);
                    }
                }
            });
            tvContent.setMaxLines(3);
            tvContent.setEllipsize(TextUtils.TruncateAt.END);
            tvForwardContent.setMaxLines(3);
            tvForwardContent.setEllipsize(TextUtils.TruncateAt.END);

        }

        /**
         * 全屏幕按键处理
         */
        private void resolveFullBtn(final StandardGSYVideoPlayer standardGSYVideoPlayer) {
            standardGSYVideoPlayer.startWindowFullscreen(context, true, true);
        }

    }
}
